<template>
    <a-spin :spinning="spinning" tip="登录中，请稍候……">
        <div class="login">
            <div class="page-label">
                <div class="rect">登录</div>
                <div class="triangle"></div>
            </div>
            <div class="page-title">
                <div class="title">用户登录</div>
                <div class="vice-title">CUSTOMER LOGIN</div>
            </div>
            <div class="form">
                <div class="form-item">
                    <div class="label">账号（邮箱）</div>
                    <div class="input-wrap">
                        <a-input v-model="account" size="large" />
                    </div>
                </div>
                <div class="form-item">
                    <div class="label">密码</div>
                    <div class="input-wrap">
                        <a-input-password v-model="password" size="large" />
                    </div>
                </div>
                <div class="form-item" v-show="needVerifyCode">
                    <div class="label">验证码</div>
                    <div class="input-wrap">
                        <div class="img-code">
                            <img :src="imgUrl+'&random='+randoms" alt="" @click="randoms=Math.random()">
                        </div>
                        <a-input v-model="verifyCode" class="verifycode" size="large" style="width:270px;" />
                    </div>
                </div>
                <div class="form-item">
                    <a-button type="primary" size="large" class="login-btn" block @click="login">登录/LOGIN</a-button>
                    <div class="links">
                        <router-link class="f-r" to="/register">新用户注册</router-link>
                        <router-link to="/resetPwd">忘记密码？</router-link>
                    </div>
                </div>
            </div>
            
            <a-modal v-model="successVisible" wrapClassName="success-win" title="" :closable="false" centered :footer="null">
                <div class="content">
                    <a-icon type="check-circle" theme="filled" />
                    <div class="tips1">恭喜您，登录成功！</div>
                    <div class="btns">
                        <a-button type="primary" @click="goVisit(1)">去数据查询</a-button>
                        <a-button type="primary" @click="goVisit(2)">去数据录入</a-button>
                    </div>
                </div>
            </a-modal>
        </div>
    </a-spin>
</template>

<script>
import config from '../../until/configs';
import { isEmail, messageTips, setCookie} from '../../until/tools';
export default {
    name:'Login',
    data() {
        return {
            buttonText:'获取验证码',
            account:'',
            password:'',
            verifyCode:'',
            successVisible:false,
            sid:'',
            needVerifyCode:false,
            imgUrl:'',
            randoms:'',
            spinning:false
        }
    },
    methods:{

        login() {
            if(this.account==='') {
                messageTips('请输入邮箱账号',2);
                return false;
            }
            if(!isEmail(this.account)) {
                messageTips('邮箱格式不正确',2);
                return false;
            }
            if(this.password==='') {
                messageTips('请输入密码',2);
                return false;
            }
            let params = {
                account:this.account,
                password:this.password,
                loginType:'pwd',
            }
            this.$http.isNeedVerifyCode({account:params.account}).then(res=>{
                if(res.needVerifyCode) {
                    this.needVerifyCode = true;
                    if(this.verifyCode==='') {
                        messageTips('请输入验证码',2);
                        this.$http.getSign({account:this.account}).then(res=>{
                            if(res) {
                                if(res.sign) {
                                    this.imgUrl = config.imgCode+'?account='+this.account+'&sign'+res.sign;
                                }
                            }
                        });
                        return false;
                    }else {
                        params.imageCode = this.verifyCode;
                        this.continueLogin(params);
                    }
                } else {
                    this.continueLogin(params);
                }
            })
            
        },
        continueLogin(params) {
            this.spinning = true;
            this.$http.login(params).then(res=>{
                if(res) {
                    if(res.sid) {
                        setCookie('bd_sid',res.sid,{time:1});
                        this.sid = res.sid;
                        if(res.canInput===1) {
                            this.successVisible = true;
                            this.spinning = false;
                            setCookie('canInput',res.canInput,{time:1});
                        } else {
                            this.$router.push({path:'/datas_data'})
                        }                        
                    } else {
                        this.spinning = false;
                        messageTips(res.message,1);
                    }
                }
            });
        },
        goVisit(type) {
            if(type===1) {
                this.$router.push({path:'/datas_data'})
            } else {
                this.$router.push({path:'/inputs_cube'})
            }
        }
    },
    mounted() {

    }
}
</script>

<style lang="scss" scoped>
@import '../../scss/login.scss';
</style>